//
//  DetailViewController.m
//  muce
//
//  Created by  yunshao on 2018/2/7.
//  Copyright © 2018年  yunshao. All rights reserved.
//

#import "DetailViewController.h"
#import "pch.h"
#import "WebViewController.h"
#import "VideoViewController.h"
#import "TestVideoViewController.h"

#import "GuideViewController.h"
#import "ProblemViewController.h"
#import "ContactViewController.h"
#import "AboutViewController.h"
#import "LPQuestionnaireViewController.h"



@interface DetailViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{
    
}
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)UIButton * backButton;               //返回按钮
@property (nonatomic,strong)UIButton * testButton;              //测试用
@property (nonatomic,strong)UILabel * titleString;
@property(nonatomic,strong)UIButton * messageButton;            //信息点击按钮
@property(nonatomic,strong)UIScrollView * myScrollView;         //滚动
@property(nonatomic,strong)UIImageView * picImageView;          //图片
@property(nonatomic,strong)UILabel * titleLabel;                //
@property(nonatomic,strong)UILabel * countLable;                //请求内容
@property(nonatomic,strong)UIImageView *iconImageView;          //icon图标。
@property(nonatomic,strong)UILabel * objLabel;                  //剩余时长。
@property(nonatomic,strong)UIButton * startButton;              //立即测评
@property(nonatomic,strong)NSArray * titleArray;                //数据
@property(nonatomic,strong)NSMutableArray * countMArray;
@property(nonatomic,strong)NSDictionary * couentDic;            //源数据。

@property(nonatomic,strong)UITableView * myTableView;           //  表格。
@property(nonatomic,strong)NSArray * tableViewArray;            //表格类别数据

@property(nonatomic,strong)NSString * urlString;                //视频地址

@property(nonatomic,strong)UIView * tapView;                    //存放手势的view
@property(nonatomic,strong)UITapGestureRecognizer * clikcON;        //单击手势。


@property(nonatomic,assign)NSInteger  one_X;
@property(nonatomic,assign)NSInteger  one_Y;
@property(nonatomic,assign)NSInteger  two_X;
@property(nonatomic,assign)NSInteger  two_Y;                //两个点的经纬度




@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    
    
    [self requData];
    
   //  [self addMianView];
   
}
-(void)requData{
    _titleArray = @[@"项目名称：",@"项目编号：",@"节 目 方 ：",@"当前状态：",@"测评时间：",@"测评说明："];
    _tableViewArray = @[@"操作指南",@"常见问题",@"联系我们",@"关于软件",@"退出登陆"];
    _countMArray =[[NSMutableArray alloc]init];
    NSDictionary *dic = [LPCommonTool readUserDefaultObjectForKey:DSUSERKEY];
    
    
    [LPRequestParser postLoginAppUserName:[NSString stringWithFormat:@"%@",[LPCommonTool readphoneNumber]] securityCode:dic[@"password"] url:DetailsApp block:^(id responseObject, NSError *error) {
        _couentDic = [[NSDictionary alloc]init];
        
        if (responseObject) {
            _couentDic = responseObject;
            _urlString = responseObject[@"cut_address"];
            _one_X = [responseObject[@"x_axis"] integerValue];
            _one_Y =[responseObject[@"y_axis"] integerValue];
            _two_X = [responseObject[@"x1_axis"] integerValue];
            _two_Y =[responseObject[@"y1_axis"] integerValue];
            
            
            [_countMArray addObject:responseObject[@"eval_name"]];
            [_countMArray addObject:responseObject[@"eval_num"]];
            [_countMArray addObject:responseObject[@"login_name"]];
            NSString *stateTypeString;
            if ([responseObject[@"eval_state"] integerValue] == 0){
                stateTypeString = @"待测评";
            }else if ([responseObject[@"eval_state"] integerValue] == 1){
                stateTypeString = @"测评完成";
            }else if ([responseObject[@"eval_state"] integerValue] == 2){
                stateTypeString = @"测评中";
            }else if ([responseObject[@"eval_state"] integerValue] == 3){
                stateTypeString = @"待开启";
            }else if ([responseObject[@"eval_state"] integerValue] == 3) {
                stateTypeString = @"待开启";
            }else if ([responseObject[@"eval_state"] integerValue] == 4) {
                stateTypeString = @"测评中";
            }else if ([responseObject[@"eval_state"] integerValue] == 5) {
                stateTypeString = @"已过期";
            }else if ([responseObject[@"eval_state"] integerValue] == 6) {
                stateTypeString = @"以完成";
            }else{
                stateTypeString = @"其他";
            }
            [_countMArray addObject:stateTypeString];
            
            if ([[LPCommonTool readIdengit] integerValue] == 1) {
              //  [_countMArray addObject:@"暂无"];
                if (responseObject[@"eval_description"]) {
                    [_countMArray addObject:@"暂无"];
                }else{
                    [_countMArray addObject:responseObject[@"eval_description"]];
                }
                
                if (responseObject[@"open_time"]) {
                    [_countMArray addObject:@"暂无"];
                }else{
                     [_countMArray addObject:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@至%@",[LPCommonTool dateWithTimeIntervalSince1970:[responseObject[@"open_time"] longValue] formatter:@"yyyy-MM-dd"],[LPCommonTool dateWithTimeIntervalSince1970:[responseObject[@"open_time"]longValue] + 2592000000 formatter:@"yyyy-MM-dd"]]]];
                }
                
               

                
            }else if ([[LPCommonTool readIdengit] integerValue] == 2){
                [_countMArray addObject:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@至%@",[LPCommonTool dateWithTimeIntervalSince1970:[responseObject[@"open_time"] longValue] formatter:@"yyyy-MM-dd"],[LPCommonTool dateWithTimeIntervalSince1970:[responseObject[@"open_time"]longValue] + 2592000000 formatter:@"yyyy-MM-dd"]]]];
                 [_countMArray addObject:responseObject[@"eval_description"]];
            }
            
           
          // [_countMArray addObject:@"测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试测试"];
             [self addMianView];
        }
    }];
    
}
-(void)addMianView{
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DSDevice_W, DSFixedPX(110))];
    _backView.backgroundColor = DSColorWithHex(0x3279ca);
    _backView.userInteractionEnabled = YES;
    [self.view addSubview:_backView];
    
   

    
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(DSFixedPX(20), DSFixedPX(45), DSFixedPX(40), DSFixedPX(40));
    [_backButton setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:_backButton];
    
    
    _testButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _testButton.frame = CGRectMake(DSDevice_W - DSFixedPX(100), DSFixedPX(45), DSFixedPX(40), DSFixedPX(40));
    [_testButton setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
    [_testButton addTarget:self action:@selector(testButtonClicked) forControlEvents:UIControlEventTouchUpInside];
   // [_backView addSubview:_testButton];
    
    
    
    
    _titleString = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(40), DSFixedPX(40), DSDevice_W - DSFixedPX(80), DSFixedPX(50))];
    _titleString.text = @"测评详情";
    _titleString.textAlignment = NSTextAlignmentCenter;
    _titleString.font = DSFontSize(16);
    _titleString.textColor = DSColorWithHex(0xffffff);
    [_backView addSubview:_titleString];

    
    _messageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _messageButton.frame = CGRectMake(DSDevice_W - DSFixedPX(20) - DSFixedPX(40), DSFixedPX(40), DSFixedPX(40), DSFixedPX(40));
    [_messageButton setImage:[UIImage imageNamed:@"icon_instructions"] forState:UIControlStateNormal];
    [_messageButton addTarget:self action:@selector(messageButtonClicked) forControlEvents:UIControlEventTouchUpInside];
   // [_backView addSubview:_messageButton];
    _myScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_backView.frame), DSDevice_W, DSDevice_H - DSFixedPX(100) - DSFixedPX(200))];
   // _myScrollView.contentSize = CGSizeMake(DSDevice_W, DSDevice_H);
    _myScrollView.userInteractionEnabled = YES;
    [self.view addSubview:_myScrollView];
    
    _picImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, DSDevice_W, DSFixedPX(400))];
    _picImageView.image = [UIImage imageNamed:@"login_bg"];
    NSString * picUrlString = [NSString stringWithFormat:@"%@%@",@"http://222.249.163.48:8080/",_couentDic[@"pic_address"]];
    [_picImageView yy_setImageWithURL:[NSURL URLWithString:picUrlString] placeholder:[UIImage imageNamed:@"login_bg"]];
    
    ;
    
    
    [_myScrollView addSubview:_picImageView];
    for (int i = 0; i<_titleArray.count; i++) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(DSFixedPX(70), CGRectGetMaxY(_picImageView.frame) + DSFixedPX(40) + i * DSFixedPX(70), DSFixedPX(0), DSFixedPX(40))];
;
        _titleLabel.text = [NSString stringWithFormat:@"%@",_titleArray[i]];
       // _titleLabel.font = DSFontSize(18);
       // _titleLabel.numberOfLines = 0;
        [_titleLabel sizeToFit];
        _titleLabel.textColor = DSColorWithHex(0x424242);
        [_myScrollView addSubview:_titleLabel];
        
        _countLable = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_titleLabel.frame) + DSFixedPX(10), CGRectGetMaxY(_picImageView.frame) + DSFixedPX(40) + i * DSFixedPX(70), DSDevice_W -CGRectGetMaxX(_titleLabel.frame)  - DSFixedPX(20), DSFixedPX(40))];
        _countLable.text =[NSString stringWithFormat:@"%@",_countMArray[i]] ;
        _countLable.textColor = DSColorWithHex(0x424242);
        _countLable.font = DSFontSize(16);
        _countLable.numberOfLines = 0;
        if (i == _titleArray.count - 1) {
            [_countLable sizeToFit];
        }
        [_myScrollView addSubview:_countLable];
    }
    
     _myScrollView.contentSize = CGSizeMake(DSDevice_W, DSDevice_H - DSFixedPX(200));
    
  
    
    _objLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,0,0,0)];
    _objLabel.textColor = DSColorWithHex(0x424242);
    _objLabel.font = DSFontSize(16);

    if ([_couentDic[@"surplus_time"] isKindOfClass:[NSNull class]]) {
        _objLabel.text = @"暂无";
    }else{
        _objLabel.text =[NSString stringWithFormat:@"剩余%@结束测评",_couentDic[@"surplus_time"]];
    }
        [_objLabel sizeToFit];
    _objLabel.center = CGPointMake(DSDevice_W/2.0f, DSDevice_H - DSFixedPX(150) + CGRectGetHeight(_objLabel.frame)/2.0f);
    [self.view addSubview:_objLabel];
    
    _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMinX(_objLabel.frame)- DSFixedPX(50),CGRectGetMinY(_objLabel.frame), DSFixedPX(35), DSFixedPX(35))];
    _iconImageView.image = [UIImage imageNamed:@"icon_clock"];
    [self.view addSubview:_iconImageView];
    
    _startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _startButton.frame = CGRectMake(DSFixedPX(100), DSDevice_H - DSFixedPX(100), DSDevice_W - DSFixedPX(200), DSFixedPX(90));
    _startButton.backgroundColor = [UIColor redColor];
    _startButton.layer.masksToBounds = YES;
    _startButton.layer.cornerRadius = DSFixedPX(30);
    [_startButton setTitle:@"立即测评" forState:UIControlStateNormal];
    [_startButton setTitleColor:DSColorWithHex(0xffffff) forState:UIControlStateNormal];
    [_startButton addTarget:self action:@selector(startButtonCliced) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_startButton];
   
    
    
    _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_backView.frame), DSDevice_W / 2 , DSDevice_H - _backView.frame.size.height) style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.hidden = YES;
    _myTableView.backgroundColor = DSColorWithHex(0x5d9feb);
    _myTableView.rowHeight = DSFixedPX(75);
    _myTableView.alpha = 0.85f;
    [self.view addSubview:_myTableView];
    
    self.tapView = [[UIView alloc]initWithFrame:CGRectMake(DSDevice_W / 2, CGRectGetMaxY(_backView.frame), DSDevice_W / 2, DSDevice_H - _backView.frame.size.height)];
    self.tapView.hidden = YES;
    self.tapView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tapView];
    
    
    _clikcON = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapObj)];
    [self.tapView addGestureRecognizer:_clikcON];
}

-(void)tapObj{
    self.myTableView.hidden = YES;
    _backButton.selected = NO;
    self.tapView.hidden = YES;
    _myScrollView.userInteractionEnabled = YES;
    
}
//测试按钮
-(void)testButtonClicked{
    LPQuestionnaireViewController * artVc = [[LPQuestionnaireViewController alloc]init];
    [self presentViewController:artVc animated:YES completion:nil];
}




-(void)startButtonCliced{
    
    TestVideoViewController * TESTV= [[TestVideoViewController alloc]init];
    TESTV.addressUrl =[NSString stringWithFormat:@"http://222.249.163.48:8080/%@",self.urlString];
    TESTV.titleString =[NSString stringWithFormat:@"%@",_countMArray[0]];
    TESTV.x_One = self.one_X;
    TESTV.y_One = self.one_Y;
    TESTV.x_Two = self.two_X;
    TESTV.y_Two = self.two_Y;
    [self presentViewController:TESTV animated:YES completion:nil];
 
    
    
}

-(void)messageButtonClicked{
    WebViewController * webVc = [[WebViewController alloc]init];
    [self presentViewController:webVc animated:YES completion:^{
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _tableViewArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"OBJcell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"OBJcell"];
        cell.textLabel.text = [NSString stringWithFormat:@"%@",_tableViewArray[indexPath.row]];
        cell.backgroundColor = DSColorWithHex(0x5d9feb);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.textLabel.textColor = DSColorWithHex(0xffffff);
        cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon%ld",indexPath.row]];
        cell.textLabel.font =DSFontSize(13);
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *urlArray = @[@"http://222.249.163.39:8080/app/IosOperation.html",@"http://222.249.163.39:8080/app/IosFAQ.html"
                          ,@"http://222.249.163.39:8080/app/IosContact.html",@"http://222.249.163.39:8080/app/IosAbout.html"];
    NSArray *titleArray =@[@"操作指南",@"常见问题",@"联系我们",@"关于软件"];
    
    
    if (indexPath.row == 0 || indexPath.row == 1 ||indexPath.row == 2 ||indexPath.row == 3 ) {
        
        WebViewController * webVc = [[WebViewController alloc]init];
        webVc.urlString = [NSString stringWithFormat:@"%@",urlArray[indexPath.row]];
        webVc.titleString = [NSString stringWithFormat:@"%@",titleArray[indexPath.row]];
        [self presentViewController:webVc animated:YES completion:^{
            
        }];
    }
//    else if (indexPath.row == 1){
//        ProblemViewController * probleVc = [[ProblemViewController alloc]init];
//        [self presentViewController:probleVc animated:YES completion:^{
//
//        }];
//
//    }else if (indexPath.row == 2){
//        ContactViewController * contactVc = [[ContactViewController alloc]init];
//        [self presentViewController:contactVc animated:YES completion:^{
//
//        }];
//
//    }else if (indexPath.row == 3){
//        AboutViewController * aboutVc = [[AboutViewController alloc]init];
//        [self presentViewController:aboutVc animated:YES completion:^{
//
//        }];
//
//    }
    else if (indexPath.row == 4){
        
        UIAlertView *WXinstall=[[UIAlertView alloc]initWithTitle:@"" message:@"确认要注销登录么？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];//一般在if判断中加入
        [WXinstall show];
    }
}

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
 {
     if (buttonIndex == 0) {
           [alertView dismissWithClickedButtonIndex:0 animated:YES];
     }else if (buttonIndex == 1){
         [self dismissViewControllerAnimated:YES completion:nil];
         
         NSMutableDictionary * histerDic = [[NSMutableDictionary alloc]init];
         [histerDic setValue:@(0) forKey:@"historyTime"];
         [LPCommonTool writeHisteryTimeObject:histerDic forKey:DSHISTERY];
     }
}

-(void)backButtonClicked:(UIButton *)sender{
    
    if (sender.selected == YES) {
        _myTableView.hidden = YES;
        self.tapView.hidden = YES;
        _myScrollView.userInteractionEnabled = YES;
        sender.selected = NO;
    }else{
       _myTableView.hidden = NO;
        self.tapView.hidden = NO;
        _myScrollView.userInteractionEnabled = NO;          //关闭点击效果
        sender.selected = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//-(BOOL)shouldAutorotate{
//    return NO;
//}
//-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
//    return UIInterfaceOrientationMaskAll;
//    
//}
//-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
//    return UIInterfaceOrientationPortrait;
//}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
